<template>
	<!-- 条件筛选页面 -->
	<view class="container">
		<view class="top">
			<view class="search flex-between">
				<input type="text" placeholder="请输入关键词搜索" v-model="keyWord">
				<view class="btn tc" @click="getData">
					搜索
				</view>
			</view>
			{{tipName}}名称
		</view>
		<view class="list">
			<van-radio-group :value="values " @change="onChange">
				<van-radio name="all">全部</van-radio>
				<van-radio v-for="(item, index) in list" :key="index" :name="item.value">{{item.label}}</van-radio>
			</van-radio-group>
			<view class="tc" style="color: #999999; line-height: 60rpx;">没有更多了</view>
		</view>
		<view class="bom-btn flex-between">
			<view class="btn reset" @click="reset">重置</view>
			<view class="btn submit" @click="submit">确定</view>
		</view>
	</view>
</template>

<script>
	import {
		loading,
		loaded
	} from '@/utils/tip.js'
	export default {
		data() {
			return {
				values: '',
				obj: null,
				list: [],
				keyWord: '',
				type: '',
				labelType: '',
				request: '', // 请求的接口名称
			};
		},
		onLoad(options) {
			console.log('options', options)
			this.type = options.type
			this.values = options.values
			this.labelType = options.labelType
			this.getData()
		},
		computed: {
			tipName() {
				let name = ''
				switch (this.type) {
					case 'taobaoId':
						name = '联盟账号'
						break;
					case 'staffId':
						name = '运营人'
						break;
					case 'label':
						name = '标签'
						break;
					case 'positionId':
						name = '推广位'
						break;
				}
				uni.setNavigationBarTitle({
					title: '选择' + name
				});
				return name
			}
		},
		methods: {
			switchRequest() {
				switch (this.type) {
					case 'positionId':
						this.request = 'pid'
						break
					case 'taobaoId':
						this.request = 'taobao'
						break;
					case 'staffId':
						this.request = 'staffList'
						break;
					case 'label':
						this.labelType === 'position' ? this.request = 'pidLabelList' : this.request = 'chanLabelList'
						break;
				}
			},
			onChange(e) {
				this.values = e.detail
				if (e.detail === 'all') {
					this.obj = {
						value: '',
						label: this.tipName
					}
					return
				}
				this.list.forEach((item, list) => {
					if (item.value == e.detail) {
						this.obj = item
					}
				})
			},
			submit() {
				uni.setStorageSync(this.type, this.obj)
				uni.navigateBack({
					delta: 1
				})
			},
			reset() {
				this.values = 'all';
				wx.pageScrollTo({
					scrollTop: 0
				})
			},
			getData() {
				this.switchRequest()
				loading()
				this.$service.common[this.request]({
					keyWord: this.keyWord
				}).then((res) => {

					let arr = []
					res.forEach((item, index) => {
						let obj = {}
						switch (this.type) {
							case 'positionId':
								obj.value = item.positionId
								obj.label = item.positionName
								break
							case 'taobaoId':
								obj.value = item.id
								obj.label = item.account
								break;
							case 'staffId':
								obj.value = item.id
								obj.label = item.username
								break;
							case 'label':
								obj.value = item
								obj.label = item
								break;
						}
						arr.push(obj)
					})
					this.list = arr
					loaded()
				}).catch((err) => {
					loaded()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding-top: 48rpx;
		background-color: white;

		.top {
			color: #999999;
			padding: 0 24rpx;
			padding-bottom: 14rpx;
			border-bottom: 2rpx solid #F2F2F2;

			.search {
				margin-bottom: 26rpx;

				input {
					width: 522rpx;
					height: 72rpx;
					border: 2rpx solid #F2F2F2;
					color: #999999;
					padding-left: 24rpx;
					box-sizing: border-box;
					border-radius: 8rpx;
				}

				.btn {
					width: 160rpx;
					height: 72rpx;
					background-color: #0C5DFF;
					color: white;
					line-height: 72rpx;
					border-radius: 8rpx;
					font-size: 28rpx;
				}
			}
		}

		.list {
			padding: 0 24rpx;
			padding-bottom: 154rpx;

			/deep/.van-radio {
				height: 72rpx;
				align-items: center;
				border-bottom: 2rpx solid #F2F2F2;

				.van-radio__icon--checked {
					background-color: $main-color;
					border: none;
				}
			}
		}

		.bom-btn {
			padding: 40rpx 126rpx;
			width: 100%;
			height: 152rpx;
			background-color: white;
			position: fixed;
			bottom: 0;
			left: 0;

			.btn {
				width: 240rpx;
				height: 72rpx;
				border-radius: 42rpx;
				line-height: 72rpx;
				text-align: center;
				font-size: 28rpx;
			}

			.reset {
				border: 2rpx solid #FFECE5;
				color: $main-color;
			}

			.submit {
				background-color: $main-color;
				color: white;
			}
		}
	}
</style>
